package com.android.jumishangcheng.fragment.member;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.jumishangcheng.R;
import com.android.jumishangcheng.fragment.BaseLazyFragment;
import com.android.jumishangcheng.fragment.group.Fragment_SCMain_TabList;
import com.android.jumishangcheng.model.group.SameCityMainData;
import com.android.jumishangcheng.model.group.SameCityMainDataCallback;
import com.android.jumishangcheng.utils.ConstantsUrl;
import com.android.jumishangcheng.utils.HttpUtils;
import com.android.jumishangcheng.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * Created by Administrator on 2017/6/9.
 */
public class Fragment_SameCity_Service extends BaseLazyFragment {
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager viewPager;
//    @BindView(R.id.main_top_title)
//    TextView mainTopTitle;
//    @BindView(R.id.main_top_citychoose)
//    TextView main_top_citychoose;
//    @BindView(R.id.main_top_bg)
//    Toolbar mainTopBg;

    private static final String ARG_PARAM1 = "param1";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private ArrayList<SameCityMainData.DataBean.Cat_ListBean> cat_list = new ArrayList<>();
    private GroupFragmentPagerAdapter pagerAdapter;
    private int page = 1;
    private MyReceiver receiver;

    public Fragment_SameCity_Service() {
    }

    public static Fragment_SameCity_Service newInstance() {
        Fragment_SameCity_Service fragment = new Fragment_SameCity_Service();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        this.inflater = inflater;
//        view = inflater.inflate(R.layout.fragment_samecitymain, container, false);
//        mcontext = getActivity();
//        ButterKnife.bind(this, view);
////        mainTopTitle.setText("同城商品");
////        mainTopTitle.setTextColor(Color.parseColor("#000000"));
//        // main_top_citychoose.setTextColor(Color.parseColor("#000000"));
//        //mainTopBg.setBackgroundColor(Color.parseColor("#ffffff"));
//        PostCityChoose();
//        GetSCMain();
//        return view;
//    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        if (networkConnected) {
            GetSCMain();
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_samecitymain, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
//        mainTopTitle.setText("同城商品");
//        mainTopTitle.setTextColor(Color.parseColor("#000000"));
        // main_top_citychoose.setTextColor(Color.parseColor("#000000"));
        //mainTopBg.setBackgroundColor(Color.parseColor("#ffffff"));
        receiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_LOCATION_CHANGE);
        mcontext.registerReceiver(receiver, intentFilter);
        initUI();
        return view;
    }

    private void initUI() {
        pagerAdapter = new GroupFragmentPagerAdapter(mcontext.getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private class GroupFragmentPagerAdapter extends FragmentPagerAdapter {

        public GroupFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment_SCMain_TabList.newInstance(cat_list.get(position).getId());
        }

        @Override
        public int getCount() {
            return cat_list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cat_list.get(position).getCatname();
        }
    }

    @OnClick({R.id.click_resetnetwork_refresh})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                GetSCMain();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void GetSCMain() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.SAMECITYMAIN;

        if (networkConnected) {
            HashMap<String, String> map = new HashMap<>();
            map.put("cid", "0");
            HttpUtils.get(address, map, new SameCityMainDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SameCityMainData response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        cat_list.clear();
                        SameCityMainData.DataBean data = response.getData();
                        if (data != null) {
                            noData.setVisibility(View.GONE);
                            List<SameCityMainData.DataBean.Cat_ListBean> catListBeen = data.getCat_list();
                            cat_list.add(new SameCityMainData.DataBean.Cat_ListBean("0", "同城到家"));
                            if (ObjectUtils.listHasData(data.getCat_list())) {
                                cat_list.addAll(catListBeen);
                            }
//                            initUI();
                            pagerAdapter.notifyDataSetChanged();
                        }
                    } else {
                        noData.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "连接连接出现异常");
        }
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            initData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mcontext.unregisterReceiver(receiver);
    }
}
